package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.dao.DeptInfo;
import com.tky.conference.dao.HysInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.interfaces.HysInfoInter;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cll on 2015/12/17.
 */
public class HysInfoImpl implements HysInfoInter{
    private String TAG = "HysInfoImpl";
    private HysInfo hysInfobj;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public HysInfoImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void hysInfo(String account) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params=new RequestParams();
        params.put("account", account);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.get(UrlConstant.HYS,params,  new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                System.out.println("success");
                Log.i(TAG, "succ");
                JSONArray jsonArr;
                JSONObject jsonObject;
                JSONObject failjsonobj;
                String data = null;
                try {
                    String str = new String(bytes, "UTF-8");
                    Log.i(TAG, str);
                    jsonObject= new JSONObject(str);
                    String s = jsonObject.getString("status");
                    Log.i(TAG, s);
                    if (Integer.parseInt(s) >0) {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus(jsonObject.getString("status"));
                        failRequestobj.setMsg(jsonObject.getString("msg"));
                        httpResultListener.onResult(failRequestobj);
                    }else{
                        String dataobj=jsonObject.getString("data");
                        Log.i(TAG, dataobj);
                        JSONObject datao=new JSONObject(dataobj);
                        JSONArray dataarr=new JSONArray(datao.getString("list"));
                        Log.i(TAG, dataarr.toString());
                        List<HysInfo> hysInfoList=new ArrayList<HysInfo>();
                        for (int j = 0; j < dataarr.length(); j++) {
                            data = dataarr.get(j).toString();
                            jsonObject = new JSONObject(data);
                            hysInfobj=new HysInfo();
                            hysInfobj.setId(jsonObject.getString("id"));//会议室id
                            hysInfobj.setPid(jsonObject.getString("pid"));//会议名称
                            hysInfobj.setName(jsonObject.getString("name"));//会议地址
                            hysInfoList.add(hysInfobj);
                        }
                        httpResultListener.onResult(hysInfoList);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                try {
                    System.out.println("fail");
                    try {
                        failRequestobj = new FailRequest();
                        failRequestobj.setStatus("-1");
                        failRequestobj.setMsg("网络异常");
                        httpResultListener.onResult(failRequestobj);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onStart() {
                super.onStart();
                httpResultListener.onStartRequest();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                httpResultListener.onFinish();
            }
        });
    }
}
